<div class="content">
	<div class="tab_bar">
		<ul>
			<li class="tab_index" onclick="setTabContent(this);">新建文章</li>
		</ul>
		<button onclick="submit(this)" class="btn_tabbar submit_it" return="article/list.do?currentPageNo=1">发 布</button>
		<button onclick="save(this)" class="btn_tabbar submit_it" return="article/list.do?currentPageNo=1">保存草稿</button>
	</div>
	<form id="myForm" action="article/save.do" method="post" onsubmit="return false;">
	<input id="status" name="status" value="0" type="hidden" />
	<div class="tab_content">
		<div class="tab_item tab_show"> 
			<table class="table_for_add">
				<tbody>
					<tr class="rowBgColor">
						<th width="140">文章类别：</th>
						<td>
							<select class="select_for_add" name="cid">
					  			<@categoryTree id = 0 node = -1/> 
							</select>
						</td>
					</tr>
					<tr>
						<th>文章标题： </th>
						<td><input id="title" class="input_for_add" name="title" type="text" placeholder="请输入文章标题" /></td>
					</tr>
					<tr class="rowBgColor">
						<th>文章图标： </th>
						<td><input  class="input_for_add" name="icon" type="text" />
						</td>
					</tr>
					<tr>
						<th>内容类型： </th>
						<td>
							<input id="statistics_1" name="type" type="radio" value="0" checked="checked" /><label for="statistics_1">&nbsp;HTML&nbsp; &nbsp;</label>
							<input id="statistics_2" name="type" type="radio" value="1" /><label for="statistics_2">&nbsp;text/markdown&nbsp; &nbsp;</label>
		
							<span class="tip_text">切换类型将会清空内容哦!</span>
						</td>
						<td></td>
					</tr>
					<tr>
				    	<th>文章内容：</th>
						<td></td>
					</tr> 
				    <tr>
						<td colspan="2" >
							<script type="text/plain" id="mushroomeditor" name="content"  style="height:350px;"></script>
						</td>
					</tr>
					<tr class="rowBgColor">
						<th>文章关键字：</th>
						<td><input id="keywords" class="input_for_add" name="keywords" type="text" /><a onclick="keyword()">自动获取关键字</a></td>
					</tr>
					<tr>
						<th>文章描述：</th>
						<td><textarea class="textarea_for_add" name="description"></textarea></td>
					</tr> 
					<tr class="rowBgColor">
						<th>文章作者：</th>
						<td><input class="input_for_add" name="author" type="text" value="${loginusername!}" /></td>
					</tr>
					<tr class="rowBgColor">
						<th>原文地址：</th>
						<td><input class="input_for_add" name="source" type="text" value="" /></td>
					</tr> 
				</tbody>
			</table>
		</div>
	</div>
	</form>
</div>
<script type="text/javascript">

//初始化UE编辑器
$(function(){
	$("input[name='type']").click(function(){
 		editor.execCommand( 'source');
		if($(this).val() == 1){
 	 		editor.setDisabled();
		}else{ 
			editor.setEnabled();
		}
	 	editor.setContent('');
	});
	
	
	
	if(window.editor != null){
		window.editor.destroy();
	}

    //实例化编辑器
    //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
    window.editor = UE.getEditor('mushroomeditor');
 	
 	window.editor.ready(function(){
 		$("#title").focus();
 		
 		var status = editor.queryCommandState( 'source' );
 		if(status != 1){
 			
 		}

 		
 	});
 	 
});


function submit(obj){
	$("#status").val("1");
	window.editor.sync();//手动提交需要手动同步编辑器数据
	
	submitActionForm(obj); 
	
}


function save(obj){
	$("#status").val("0");
	window.editor.sync();//手动提交需要手动同步编辑器数据
	
	submitActionForm(obj); 
	
}

 
	$('#myForm').validator({
	    // 传入字段集合
	    fields: {
	    	"title": "required" 
	    }
	}).on("click", ".submit_it", function(){
	   // $('#myForm').trigger("submit");
	    
	 	window.editor.sync();//手动提交需要手动同步编辑器数据
		submitActionForm(obj); 
	    
	});
	
	
 



/**
 * 关键字获取接口
 * */
function keyword(){
	var title = $("#title").val();
	var content = window.editor.getContent(); 
	$.ajax({
		url:"../keywords.do",
		type:"POST",
		data: "title="+title+"&content="+content,
		dataType:"json",
		success:function(data){
			$("#keywords").val(data);
		}
	});
	
}
</script>

<#macro categoryTree id node>
	<#local temp = node + 1 />
	<#list categorys as x>
	  	<#if id = x.pid>
	  	<option value="${x.id?c}"><#list 0..temp as i> &nbsp; &nbsp; </#list>├ ${x.name!}</option>
	    <@categoryTree id = x.id node=temp/>
      	</#if>
	</#list> 
</#macro>

